package com.wzcl.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

        @Bean
        public Docket createRestApi() {
            /**
             * 在Swagger测试服务中设置，携带token访问受保护的API
             * */
            ParameterBuilder ticketPar = new ParameterBuilder();
            List<Parameter> pars = new ArrayList<Parameter>();
            /*name为token键值对中键名*/
            ticketPar.name("access_token").description("用户令牌")//自定义的参数
                    /*此处指定参数的类型，以及参数存放的位置，query、header等*/
                    .modelRef(new ModelRef("string")).parameterType("query")
                    .required(false).build();
            pars.add(ticketPar.build());

            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.wzcl.app"))
                    .paths(PathSelectors.any())
                    .build()
                    /*添加全局配置，受保护API*/
                    .globalOperationParameters(pars);
        }

        @SuppressWarnings("deprecation")
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("望子成龙网校")
                    .description("后端Rest接口")
                    .contact("测试接口")
                    .version("1.0")
                    .build();
        }

}

